<?php
	
	require_once(INCLUDE_PATH.'db.inc.php');
	
	class Admin extends DBSQL {
		
		public $_tbName = "admin_tb";	    //定义调查表名称
		private $_admineId;
		private $_name;
		private $_password;
		private $_lastLoginDate;
		private $_email;
		private $_note;

		
		public function __construct(){
			
			parent::__construct();
				
		}
		
		/**
		  * 功能：检索所有管理员
		  * 返回：数组
		 */
		 public function getAdminList(){
			 
			$sql = "SELECT * FROM ".$this->_tbName;
			return $this->select($sql); 
		 }
		
		/**
		  * 功能：提取指定表的指定ID的记录
		  * 参数：$id 表ID， $_tbName表名称
		  * 返回：数组
		 */
		 public function getInfo($id, $tbName){
			 
			 $sql = "SELECT * FROM ".$tbName." where adminId = $id";
			 $r = $this->select($sql);
			 return $r[0];
		 }
		 
		 /**
		  * 功能：提取指定表的指定ID的记录
		  * 参数：$name, $password表属性,  $_tbName表名称
		  * 返回：数组
		  */
		 public function getAdminByNameAndPassword($name, $password, $tbName){
		 
		 	$sql = "SELECT * FROM ".$tbName." where name = '$name' and password = '$password'";
		 	echo $sql."<br />";
		 	$r = $this->select($sql);
		 	return $r[0];
		 }
		 
		 /**
		   * 功能：向指定表中插入数据
		   * 参数：$name 表名称，$data 数组(格式：$data['字段名'] = 值)
		   * 返回：插入记录ID
		  */
		 public function insertData($tbName, $data){
			 
			 $field = implode(',',array_keys($data));                  //定义SQL语句的字段部分
			 $value = "";
			 $i = count($data) - 1;
			 foreach($data as $key => $val){							//组合SQL语句的值部分
				$value .= "'" . $val . "'";
				if($i > 0){                                             //判断是否到数组的最后一个值
						                            
					$value .= ",";	
					$i --;
				}		
			 }
			 
			 $sql = "INSERT INTO " . $tbName . "(" . $field . ") VALUES(" . $value . ")";
			 
			 echo $sql."<br />";
			 
		     return $this->insert($sql);
		 }
		 
		 /* 功能：更新指定表指定ID的调查表记录
		  * 参数：$name 表名称，$id 表ID，$data 数组(格式：$data['字段名'] = 值)
		  * 返回：TRUE OR FALSE
		 */
		 public function updateData($tbName,$id,$data){	 
			 $col = array();
			 foreach ($data as $key => $value){
				 $col[] = $key . "='" . $value . "'";
			 }
			 $sql = "UPDATE " . $tbName . " SET " . implode(',',$col) . " WHERE adminId = $id";
			 return $this->update($sql);
		 }
		
		/**
		  * 功能：删除指定ID的调查表记录及相关表记录
		  * 参数：$id 调查表ID
		  * 返回：TRUE OR FALSE
		*/
		public function delData($id){
			$this->begintransaction();
			try{

				$sql = "DELETE FROM " . $this->_tbName . " WHERE adminId = " . $id;
				$this->delete($sql);
			}catch(Exception $e){
				$this->rollback();
				return false;
			}
			$this->commit();
			return true;
		}

	}



?>